import 'package:flutter/material.dart';
import 'package:flutter_soundmagus/page/save/save_controller.dart';
import 'package:flutter_soundmagus/util/util.dart';
import 'package:flutter_soundmagus/widget/alert/alert_define.dart';
import 'package:flutter_swipe_action_cell/flutter_swipe_action_cell.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';

class PageSave extends StatelessWidget {
  PageSave({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(SaveController());
    return UnconstrainedBox(
      child: Material(
        color: Colors.transparent,
        child: Container(
          width: 480.w,
          height: Get.height - 80,
          decoration: kAlertBoxDecoration,
          child: Column(
            children: [
              SizedBox(
                height: 50,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Expanded(
                      child: GestureDetector(
                        child: Image.asset(
                          'al_save_basic'.img,
                          width: 338 * 0.35,
                          height: 95 * 0.35,
                        ),
                        onTap: controller.onBasicPress,
                      ),
                    ),
                    VerticalDivider(
                      width: 1,
                      color: Colors.white,
                    ),
                    Expanded(
                      child: GestureDetector(
                        child: Image.asset(
                          'al_save_advanced'.img,
                          width: 383 * 0.35,
                          height: 95 * 0.35,
                        ),
                        onTap: controller.onAdvancedPress,
                      ),
                    ),
                  ],
                ),
              ),
              //列表
              Expanded(
                child: ValueListenableBuilder<Box>(
                  valueListenable: Hive.box('settings').listenable(),
                  builder: (context, box, widget) {
                    return FutureBuilder<List<SaveItem>>(
                        future: controller.getAll(),
                        builder: ((context, snapshot) {
                          final list = snapshot.data ?? [];
                          return ListView(
                            padding: EdgeInsets.zero,
                            children: list.map((e) {
                              final index = list.indexOf(e);
                              return SwipeActionCell(
                                key: ObjectKey(list[index]),

                                ///this key is necessary
                                trailingActions: <SwipeAction>[
                                  SwipeAction(
                                      title: "delete",
                                      onTap: (CompletionHandler handler) async {
                                        controller.delete(name: e.name);
                                      },
                                      color: Colors.red),
                                ],
                                child: Material(
                                  color: Colors.white,
                                  child: ListTile(
                                    leading: Icon(
                                      Icons.lock,
                                      size: 12,
                                      color: e.usePwd
                                          ? Colors.black
                                          : Colors.transparent,
                                    ),
                                    onTap: () => controller.onItemPress(e),
                                    minLeadingWidth: 0,
                                    title: Text(e.name),
                                    subtitle: Text(
                                        e.dateTime.toString().substring(0, 19)),
                                    trailing: GestureDetector(
                                      onTap: () => controller.onShare(e),
                                      child: Container(
                                        color: Colors.white,
                                        width: 44.w,
                                        child: 'al_save_upload'.uiimg,
                                      ),
                                    ),
                                    dense: true,
                                  ),
                                ),
                              );
                            }).toList(),
                          );
                        }));
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
